//------------------------------------------------------------------------------
// <copyright file="DataGridProperties.cs" company="Damian Antonowicz">
//       copyright © 2010 Damian Antonowicz
// </copyright>
// <author>Damian Antonowicz</author>
// <email>poczta@damianantonowicz.pl</email>
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using Microsoft.Windows.Controls;

namespace DotBeer.UI.WPFClient
{
    public class DataGridProperties
    {
        public static readonly DependencyProperty MaxRowsProperty =
            DependencyProperty.RegisterAttached("MaxRows",
                                                typeof(int),
                                                typeof(DataGrid),
                                                new PropertyMetadata(new PropertyChangedCallback(MaxRows_Changed)));

        public static void SetMaxRows(DependencyObject d, int maxRows)
        {
            d.SetValue(MaxRowsProperty, maxRows);
        }

        public static int GetMaxRows(DependencyObject d)
        {
            return (int)d.GetValue(MaxRowsProperty);
        }

        private static void MaxRows_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DataGrid dataGrid = d as DataGrid;

            if (dataGrid != null)
            {
                dataGrid.BeginningEdit += (object sender, DataGridBeginningEditEventArgs editArgs) =>
                    {
                        if (dataGrid.SelectedIndex > (int)e.NewValue - 1)
                            editArgs.Cancel = true;
                    };
            }
        }
    }
}
